package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.dao.*;
import com.wing.product.model.entity.*;
import com.wing.product.model.response.WarehouseMaterialRequisitionRecordDetailVO;
import com.wing.product.model.response.WarehouseMaterialRequisitionRecordVO;
import com.wing.product.model.response.WarehouseOutboundRecordVO;
import com.wing.product.model.response.WarehouseProductionSchedulingPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseMaterialRequisitionRecordServiceImpl extends ServiceImpl<WarehouseMaterialRequisitionRecordDao, WarehouseMaterialRequisitionRecord> implements WarehouseMaterialRequisitionRecordService {

    @Autowired
    WarehouseMaterialRequisitionRecordDetailService warehouseMaterialRequisitionRecordDetailService;
    @Autowired
    WarehouseMaterialRequisitionRecordDetailDao warehouseMaterialRequisitionRecordDetailDao;
    @Autowired
    WarehouseProductionSchedulingPlanDetailService warehouseProductionSchedulingPlanDetailService;
    @Autowired
    WarehouseProductionSchedulingPlanDao warehouseProductionSchedulingPlanDao;
    @Autowired
    WarehouseProductionSchedulingPlanDetailDao warehouseProductionSchedulingPlanDetailDao;
    @Autowired
    WarehouseProductInventoryTypeDao warehouseProductInventoryTypeDao;
    @Autowired
    WarehouseOutboundRecordService warehouseOutboundRecordService;

    @Override
    public Page<WarehouseMaterialRequisitionRecordVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseProductionSchedulingPlanVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WarehouseMaterialRequisitionRecordVO selectMaterialRequisitionRecord(String id) {

        WarehouseMaterialRequisitionRecordVO warehouseMaterialRequisitionRecordVO = this.baseMapper.selectMaterialRequisitionRecord(id);
//        LambdaQueryWrapper<WarehouseMaterialRequisitionRecordDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(WarehouseMaterialRequisitionRecordDetail::getRequisitionRecordId, warehouseMaterialRequisitionRecordVO.getId())
//                .eq(WarehouseMaterialRequisitionRecordDetail::getIsDeleted, 0);
//        List<WarehouseMaterialRequisitionRecordDetail> warehouseMaterialRequisitionRecordDetails = warehouseMaterialRequisitionRecordDetailDao.selectList(lambdaQueryWrapper);
        List<WarehouseMaterialRequisitionRecordDetail> warehouseMaterialRequisitionRecordDetails = warehouseMaterialRequisitionRecordDetailDao.selectMaterialRequisitionById(id);
        warehouseMaterialRequisitionRecordVO.setWarehouseMaterialRequisitionRecordDetailList(warehouseMaterialRequisitionRecordDetails);

        return warehouseMaterialRequisitionRecordVO;
    }

    @Override
    @Transactional
    public JsonResult saveWarehouseMaterialRequisitionRecord(WarehouseMaterialRequisitionRecordVO warehouseMaterialRequisitionRecordVO) {

        if (ObjectUtil.isNotEmpty(warehouseMaterialRequisitionRecordVO)) {
            WarehouseMaterialRequisitionRecord warehouseMaterialRequisitionRecord = new WarehouseMaterialRequisitionRecord();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseMaterialRequisitionRecord.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseMaterialRequisitionRecordVO, warehouseMaterialRequisitionRecord, copyOptions);
            warehouseMaterialRequisitionRecord.setRequisitionRecordCode(UniqueIdGeneratorUtil.generateUniqueId("LL", "yyyyMMddHHmmss", 1000, 9999));
            warehouseMaterialRequisitionRecord.setStatus("1");
            this.baseMapper.insert(warehouseMaterialRequisitionRecord);

            //修改制令单状态
            LambdaUpdateWrapper<WarehouseProductionSchedulingPlanDetail> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(WarehouseProductionSchedulingPlanDetail::getId, warehouseMaterialRequisitionRecordVO.getProductionSchedulingPlanDetailId())
                    .eq(WarehouseProductionSchedulingPlanDetail::getIsDeleted, 0)
                    .set(WarehouseProductionSchedulingPlanDetail::getStatus, "4");
            warehouseProductionSchedulingPlanDetailService.update(lambdaUpdateWrapper);

            if (CollectionUtils.isNotEmpty(warehouseMaterialRequisitionRecordVO.getWarehouseMaterialRequisitionRecordDetailList())) {
                Integer sumGross = 0;
                Integer sumNet = 0;
                CopyOptions cpt = CopyOptions.create()
                        .setEditable(WarehouseOutboundRecordDetail.class)
                        .setIgnoreError(true)
                        .setIgnoreNullValue(true);

                List<WarehouseOutboundRecordDetail> warehouseOutboundRecordDetailList = new ArrayList<>();

                for (WarehouseMaterialRequisitionRecordDetail wr : warehouseMaterialRequisitionRecordVO.getWarehouseMaterialRequisitionRecordDetailList()) {
                    wr.setRequisitionRecordId(warehouseMaterialRequisitionRecord.getId());
                    wr.setStatus("1");
                    sumGross += wr.getGrossWeight().intValue();
                    sumNet += wr.getNetWeight().intValue();
                    //添加领料记录明细
                    WarehouseOutboundRecordDetail warehouseOutboundRecordDetail = new WarehouseOutboundRecordDetail();
                    BeanUtil.copyProperties(wr, warehouseOutboundRecordDetail, cpt);
                    warehouseOutboundRecordDetail.setProductProducer(wr.getProductProducerName());
                    warehouseOutboundRecordDetail.setIndate(wr.getStorageOrderCreateTime());
                    warehouseOutboundRecordDetailList.add(warehouseOutboundRecordDetail);
                }
                warehouseMaterialRequisitionRecordDetailService.saveBatch(warehouseMaterialRequisitionRecordVO.getWarehouseMaterialRequisitionRecordDetailList());

//                添加出库记录
                LambdaQueryWrapper<WarehouseProductionSchedulingPlanDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(WarehouseProductionSchedulingPlanDetail::getId, warehouseMaterialRequisitionRecordVO.getProductionSchedulingPlanDetailId());
                WarehouseProductionSchedulingPlanDetail warehouseProductionSchedulingPlanDetail = warehouseProductionSchedulingPlanDetailDao.selectOne(lambdaQueryWrapper);

//                查询出库类型
                LambdaQueryWrapper<WarehouseProductInventoryType> lambdaQueryWrapperType = new LambdaQueryWrapper<>();
                lambdaQueryWrapperType.eq(WarehouseProductInventoryType::getName,"领料出库");
                WarehouseProductInventoryType warehouseProductInventoryType = warehouseProductInventoryTypeDao.selectOne(lambdaQueryWrapperType);

                WarehouseOutboundRecordVO warehouseOutboundRecordVO = new WarehouseOutboundRecordVO();
                warehouseOutboundRecordVO.setTotalGrossWeight(BigDecimal.valueOf(sumGross));
                warehouseOutboundRecordVO.setTotalNetWeight(BigDecimal.valueOf(sumNet));
                warehouseOutboundRecordVO.setNumberOfMaterials(warehouseMaterialRequisitionRecordVO.getWarehouseMaterialRequisitionRecordDetailList().size());
                warehouseOutboundRecordVO.setDeliveryTime(LocalDateTime.now());
                warehouseOutboundRecordVO.setCustomerId(warehouseProductionSchedulingPlanDetail.getCustomerId());
                warehouseOutboundRecordVO.setWarehouseOutboundRecordDetailList(warehouseOutboundRecordDetailList);
                warehouseOutboundRecordVO.setInventoryTypeId(warehouseProductInventoryType.getId());
                warehouseOutboundRecordService.saveWarehouseOutboundRecord(warehouseOutboundRecordVO);

            }
            return JsonResult.success("添加成功");
        } else {
            return JsonResult.fail("添加失败");
        }
    }
}
